package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.TableManagerMapper;
import com.ld.hpfs.service.TableManagerService;

@Service
public class TableManagerServiceImpl implements TableManagerService{

	private static final Logger logger = LoggerFactory.getLogger(TableManagerServiceImpl.class);
	
	@Autowired
	private TableManagerMapper tablemanagermapper;
	
	//获取表名管理一览信息
	@Override
	public ArrayList<HashMap<String,Object>> getTableNameList(HashMap<String,Object> param)
	{
		int count = tablemanagermapper.getTableListCount(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			return tablemanagermapper.getTableList(param);
		}
	}
	
	//获取数据库中所有表名
	@Override
	public ArrayList<HashMap<String,Object>> getAllTableName(){
		return tablemanagermapper.getAllTableName();
	}
	
	//添加表名
	@Override
	public int tableManagerAdd(HashMap<String,Object> param){
		try{
			tablemanagermapper.tableManagerAdd(param);
			return 0;
		}catch(Exception ex){
			logger.error(ex.getMessage());
			return -1;
		}
	}
	
	//删除表名
		@Override
		public int tableManagerDelete(HashMap<String,Object> param){
			try{
				tablemanagermapper.tableManagerDelete(param);
				return 0;
			}catch(Exception ex){
				logger.error(ex.getMessage());
				return -1;
			}
		}
		
		//修改表名
		@Override
		public int tableManagerEdit(HashMap<String,Object> param){
			try{
				tablemanagermapper.tableManagerEdit(param);
				return 0;
			}catch(Exception ex){
				logger.error(ex.getMessage());
				return -1;
			}
		}

		
		/********************以下为履历添加 ******************************/
		 
		@Override
		public boolean addTableNameHistory(HashMap<String, Object> history_param) {
			try{
				ArrayList<HashMap<String, Object>> histories = tablemanagermapper.getTableNameForHistory((String) history_param.get("t_name"));
				for(HashMap<String, Object> history : histories){
					history.put("cpn_id", history_param.get("cpn_id"));
					history.put("action", history_param.get("action"));
					history.put("user_id", history_param.get("user_id"));
					history.put("user_name", history_param.get("user_name"));
					history.put("update_time", history_param.get("update_time"));
					tablemanagermapper.addTableNameHistory(history);
				}
				return true;
			}catch(Exception ex){
				logger.error(ex.getMessage());
				return false;
			}
		}
	}
